<?php
// +----------------------------------------------------------------------
// | 授权系统 - 客户管理控制器
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace api\modules\admin\controllers\license;

use api\modules\admin\AuthController;
use api\utils\libs\CommonAction;
use common\models\system\Customer;
use yii\db\ActiveQuery;

/**
 * 客户管理控制器
 */
class CustomerController extends AuthController
{
    use CommonAction;
    
    protected string $model = Customer::class;
    protected bool $validate = true;
    
    protected function listQuery(ActiveQuery $query): ActiveQuery
    {
        // 搜索条件已通过 QueryUtils 自动处理
        // 如果需要额外的条件，可以在这里添加
        return $query->orderBy(['id' => SORT_DESC]);
    }
    
    /**
     * 客户详情
     */
    public function actionDetail()
    {
        $id = $this->request->get('id');
        $customer = Customer::findOne($id);
        
        if (!$customer) {
            return $this->error('客户不存在');
        }
        
        return $this->success('', $customer->toArray());
    }
    
    /**
     * 创建客户
     */
    public function actionCreate()
    {
        $customer = new Customer();
        $data = $this->request->post();
        
        // 生成客户编号
        if (empty($data['customer_no'])) {
            $data['customer_no'] = 'C' . date('YmdHis') . rand(1000, 9999);
        }
        
        $data['created_by'] = $this->user_id;
        $customer->setAttributes($data);
        
        if ($customer->save()) {
            return $this->success('客户创建成功', ['id' => $customer->id]);
        }
        
        return $this->error('客户创建失败');
    }
    
    /**
     * 更新客户
     */
    public function actionUpdate()
    {
        $id = $this->request->post('id');
        $customer = Customer::findOne($id);
        
        if (!$customer) {
            return $this->error('客户不存在');
        }
        
        $data = $this->request->post();
        $data['updated_by'] = $this->user_id;
        $customer->setAttributes($data);
        
        if ($customer->save()) {
            return $this->success('客户更新成功');
        }
        
        return $this->error('客户更新失败');
    }
    
    /**
     * 删除客户
     */
    public function actionDelete()
    {
        $id = $this->request->post('id');
        $customer = Customer::findOne($id);
        
        if (!$customer) {
            return $this->error('客户不存在');
        }
        
        // 检查是否有关联的授权
        $licenseCount = \common\models\system\License::find()
            ->where(['customer_id' => $id])
            ->count();
        
        if ($licenseCount > 0) {
            return $this->error('该客户有关联的授权，无法删除');
        }
        
        if ($customer->delete()) {
            return $this->success('客户删除成功');
        }
        
        return $this->error('客户删除失败');
    }
    
    /**
     * 获取客户的授权列表
     */
    public function actionLicenses()
    {
        $customerId = $this->request->get('customer_id');
        
        $licenses = \common\models\system\License::find()
            ->where(['customer_id' => $customerId])
            ->with('product')
            ->all();
        
        $list = [];
        foreach ($licenses as $license) {
            $data = $license->toArray();
            $data['product_name'] = $license->product->product_name ?? '';
            $data['remaining_days'] = $license->getRemainingDays();
            $list[] = $data;
        }
        
        return $this->success('', ['list' => $list]);
    }
}

